package com.naiterui.ehp.bs.doctor.controller.inner;

import java.util.List;
import java.util.Map;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.naiterui.common.repository.page.PageParamVO;
import com.naiterui.common.repository.page.PageVO;
import com.naiterui.ehp.bp.bo.consult.CommentBO;
import com.naiterui.ehp.bp.bo.consult.CommentComplaintBO;
import com.naiterui.ehp.bp.bo.consult.CommentDetailBO;
import com.naiterui.ehp.bp.bo.consult.ComplaintBO;
import com.naiterui.ehp.bp.bo.consult.ComplaintDetailBO;
import com.naiterui.ehp.bp.constants.CommonConstant.ConsultType;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.doctor.service.IConsultCommentService;
import com.naiterui.ehp.bs.doctor.service.IConsultComplaintService;

import lombok.extern.slf4j.Slf4j;

/**
 * @Description 会话相关内部接口
 * @Author gongbaoqiang
 * @Date 2020/7/1 3:26 PM
 */
@Slf4j
@RestController
@RequestMapping("ad/inner/consult")
@Api(tags = {SwaggerTag.DOCTOR + SwaggerTag.INNER_API + SwaggerTag.AUTH_RECORD})
public class AdInnerConsultController {

    @Autowired
    private IConsultCommentService commentService;

    @Autowired
    private IConsultComplaintService complaintService;

    /**
     * 会话评价列表
     * @return
     * @throws BusinessException
     */
    @PostMapping("comment/list")
    public PageVO<CommentDetailBO> commentList(@RequestParam Long doctorId, PageParamVO pageParamVO) throws BusinessException{
        log.info("会话评价列表：doctorId={}", doctorId);
        return commentService.findByDoctorId(doctorId, pageParamVO);
    }

    /**
     * 提交评价
     * @return
     * @throws BusinessException
     */
    @PostMapping("comment/submit")
    public Long commentSubmit(@RequestBody CommentBO commentBO) throws BusinessException{
        log.info("提交评价：commentBO={}", commentBO.toString());
        return commentService.submit(commentBO);
    }

    /**
     * 获取医生好评率
     * @return
     * @throws BusinessException
     */
    @PostMapping("comment/praise/rate")
    public Map<Long, String> getPraiseRate(@RequestBody List<Long> doctorIds) throws BusinessException{
        log.info("获取医生好评率：doctorIds={}", doctorIds.toString());
        return commentService.getApplauseRate(doctorIds);
    }


    /**
     * 会话投诉列表
     * @return
     * @throws BusinessException
     */
    @PostMapping("complaint/list")
    public PageVO<ComplaintDetailBO> complaintList(@RequestParam Long doctorId, PageParamVO pageParamVO) throws BusinessException{
        log.info("会话投诉列表：doctorId={}", doctorId);
        return complaintService.findByDoctorId(doctorId, pageParamVO);
    }

    /**
     * 提交评价
     * @return
     * @throws BusinessException
     */
    @PostMapping("complaint/submit")
    public void complaintSubmit(@RequestBody ComplaintBO complaintBO) throws BusinessException{
        log.info("提交评价：commentBO={}", complaintBO.toString());
        complaintService.submit(complaintBO);
    }

    /**
     * 查询会话是否已经评价
     * @param consultSessionId
     * @return
     * @throws BusinessException
     */
    @PostMapping("comment/exist")
    public Boolean existCommentByConsultSession(@RequestParam("consultSessionId")Long consultSessionId, @RequestParam("consultType")Integer consultType) throws BusinessException{
        if (consultType == null) {
            consultType = ConsultType.GRAPHIC;
        }
        return commentService.existByConsultSession(consultSessionId, consultType);
    }

    /**
     * 查询会话是否已经评价
     * @param consultSessionId
     * @return
     * @throws BusinessException
     */
    @PostMapping("complaint/exist")
    public Boolean existComplaintByConsultSession(@RequestParam("consultSessionId")Long consultSessionId, @RequestParam("consultType")Integer consultType) throws BusinessException{
        if (consultType == null) {
            consultType = ConsultType.GRAPHIC;
        }
        return complaintService.existByConsultSession(consultSessionId, consultType);
    }

    /**
     * 获取会话的评价投诉
     * 
     * @param consultSessionId
     * @param consultType
     * @return
     */
    @GetMapping("getCommentComplaint")
    public CommentComplaintBO getCommentComplaint(@RequestParam("consultSessionId") Long consultSessionId,
        @RequestParam("consultType") Integer consultType) {
        CommentDetailBO commentDetailBO = commentService.getConsultSessionCommont(consultSessionId, consultType);
        ComplaintDetailBO complaintDetailBO =
            complaintService.getConsultSessionComplaint(consultSessionId, consultType);
        return CommentComplaintBO.builder().exist(commentDetailBO != null || complaintDetailBO != null)
            .commentDetail(commentDetailBO).complaintDetail(complaintDetailBO).build();
    }

}
